package com.bookrecommendation.client.database;

import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.RequestException;
import com.google.gwt.http.client.Response;
import com.google.gwt.user.client.Window;

public class RequestSaveUserBook {

	// http://jsonpfy.dinnerwithf.appspot.com/SaveDataService?
	// kind=UserBook&ID=537675235
	// &fieldsKind=String&fieldsName=name&fieldsValue=Alline Watkins
	// &fieldsKind=String&fieldsName=bookIDs&fieldsValue=105395362833449,115946728419025,

	private static String url = "http://jsonpfy.dinnerwithf.appspot.com/SaveDataService";

	public static void sendPostRequest(String userID, String username,
			String bookIDs) {

		String urlSave = url + "?kind=UserBook" + "&ID=" + userID
				+ "&fieldsKind=String&fieldsName=name&fieldsValue=" + username
				+ "&fieldsKind=String&fieldsName=bookIDs&fieldsValue="
				+ bookIDs;

		RequestBuilder builder = new RequestBuilder(RequestBuilder.POST,
				urlSave);

		// builder.setRequestData(requestData);

		try {
			Request request = builder.sendRequest(null, new RequestCallback() {

				public void onError(Request request, Throwable exception) {
					Window.alert("Fail: " + exception);
				}

				public void onResponseReceived(Request request,
						Response response) {

					if (200 == response.getStatusCode()) {
						// updateTable(asArrayOfStockData(response.getText()));
					} else {
						// displayError("Couldn't retrieve JSON ("
						// + response.getStatusText() + ")");
					}
				}
			});
		} catch (RequestException e) {
			// displayError("Couldn't retrieve JSON");
		}

	}
}
